package com.longteng.lesson2.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@Controller
@RequestMapping("/sessionController")
@SessionAttributes(value = {"userName"})
public class SessionController {

    @RequestMapping("/sessionAn")
    public ModelAndView sessionAn(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("view");
        modelAndView.addObject("age",19);
        modelAndView.addObject("userName","session");
        return modelAndView;
    }

    @RequestMapping("/setSession")
    public void setSession(HttpServletRequest request){
        HttpSession httpSession =request.getSession();
        httpSession.setAttribute("name","longteng");
        //先放在拿
        httpSession.setMaxInactiveInterval(10);
    }

    @RequestMapping("/getSessionByName")
    public @ResponseBody
    Object getSessionByName(HttpServletRequest request,String name){
        HttpSession httpSession = request.getSession();
        Object value = httpSession.getAttribute(name);
        //基本上都是map的思想
        return value;
    }
    @RequestMapping("/getSession")
    public @ResponseBody
    String getSession(HttpServletRequest request){
        HttpSession httpSession = request.getSession();
        String name =(String) httpSession.getAttribute("name");
        //基本上都是map的思想
        return name;
    }
    @RequestMapping("/getSessionByParam")
    public String getSessionByParam(HttpSession httpSession){
        httpSession.invalidate();
        return (String) httpSession.getAttribute("name");
    }
}
